<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
    <el-col :span="24" class="mb20">
      <el-form-item label="科室名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入科室名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="24" class="mb20">
      <el-form-item label="二级科室" prop="twoName">
        <el-checkbox-group v-model="form.twoName">
          <el-checkbox  :label="item" :value="item"  v-for="(item,index) in deptList" :key="index"/>
        </el-checkbox-group>
      </el-form-item>
      </el-col>

    <el-col :span="24" class="mb20">
      <el-form-item label="启用状态" prop="state">
        <el-radio-group v-model="form.state">
          <el-radio :label="0">开启</el-radio>
          <el-radio :label="1">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      </el-col>

			</el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="BloodxDirectionDeptDialog">
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/blood/bloodxDirectionDept'
import {getDeptListApi} from "/@/api/blood/bloodxCistomerDirection";
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
const deptList = ref([])

// 提交表单数据
const form = reactive({
		id:'',
	  name: '',
	  twoName:[],
	  state: 0,
});

// 定义校验规则
const dataRules = ref({
  name: [{ required: true, message: '请输入科室名称', trigger: 'blur' }],
  twoName: [{ required: true, message: '请选择二级科室', trigger: 'blur' }],
})

// 打开弹窗
const openDialog = async (id: string) => {
  visible.value = true
  await getDeptList()
  form.id = ''

  // 重置表单数据
  await nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  if (id) {
    form.id = id
    getbloodxDirectionDeptData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

  let newFormData = {
    id:'',
    name: '',
    twoName:'',
    state: '',
  }
  newFormData.id = form.id
  newFormData.name = form.name
  newFormData.state = form.state
  if(form.twoName.length == 0){
    newFormData.twoName = ''
  }else{
    newFormData.twoName = form.twoName.join(',')
  }

	try {
    loading.value = true;
		form.id ? await putObj(newFormData) : await addObj(newFormData);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getbloodxDirectionDeptData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    //将twoName转换为数组，可能是null,可能是中医,西医
    if(res.data.twoName){
      res.data.twoName = res.data.twoName.split(',')
    }else{
      res.data.twoName = []
    }
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};


const getDeptList = async () => {
 await getDeptListApi().then((res) => {
    deptList.value = res.data
  })
}

// 暴露变量
defineExpose({
  openDialog
});
</script>
